package com.example.yjhuser.controller;

import com.example.yjhuser.mapper.UserMapper;
import com.example.yjhuser.service.UserService;
import com.example.yjhuser.util.UrlUtil;
import com.yc.yjh.biz.BizException;
import com.yc.yjh.entity.Result;
import com.yc.yjh.entity.Users;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private UserMapper userMapper;

    @PostMapping("/login")
    public Result UserLogin(@RequestBody Users user){
        try {
            final Users users = userService.userLogin(user.getUsername(), user.getPassword());
            return Result.success("登录成功",users);
        } catch (BizException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/wxlogin")
    public Result WxUserLogin(@RequestBody Users user){
        try {
            final Users users = userService.wxUserLoGIN(user);
            return Result.success("登录成功",users);
        } catch (BizException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/zhuce")
    public Result WxZhuce(@RequestBody Users user){
        try {
            final Users zhuce = userService.zhuce(user);
            return Result.success("注册成功",zhuce);
        } catch (BizException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    @PostMapping("/update")
    public Result updateUserMsg(@RequestBody Users user){
        try {
            int i = userService.updateUserMsg(user);
            if (i==0){
                return Result.error("修改失败，请稍后再试!");
            }else{
                return Result.success("修改成功");
            }
        } catch (BizException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }



    @RequestMapping("/get")
    public Result getUserById(int id){
        try {
            final Users user = userService.getUserById(id);
            return Result.success("查询成功",user);
        } catch (BizException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
    @RequestMapping("/user")
    public Result selectOneUserByName(@RequestParam("username") String username){
        try {
            final Users user = userMapper.selectOneUserByName(username);
            return Result.success("查询成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    @GetMapping("/wxOAuth")
    public Result getOpenId(String code) {
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String,String> requestUrlParam = new HashMap<>();
        requestUrlParam.put("appid","wxe61a81139a1bd802");	//开发者设置中的appId
        requestUrlParam.put("secret", "69dd2e6a7faf036e6b09180998f10e4a");	//开发者设置中的appSecret
        requestUrlParam.put("js_code", code);	//小程序调用wx.login返回的code
        requestUrlParam.put("grant_type", "authorization_code");//默认参数
        return Result.success("OK",UrlUtil.sendPost(requestUrl, requestUrlParam)) ;
    }

    @Resource
    JavaMailSender javaMailSender;

    @GetMapping("/email")
    public Result sendEmail(String email){
        SimpleMailMessage smm = new SimpleMailMessage();
        //设置邮件主题
        smm.setSubject("源计划邮件验证");
        smm.setFrom("3117106059@qq.com");
        smm.setTo(email);
        Random random=new Random();
        int verify=random.nextInt(9000)+1000;

        smm.setSentDate(new Date());
        smm.setText("你的验证码为："+verify+",注意有效期为5分钟");
        javaMailSender.send(smm);
        return Result.success("发送成功!",verify);
    }


}
